package cn.wolfcode.business.controller;

import cn.wolfcode.business.domain.BusBpmnInfo;
import cn.wolfcode.business.service.IBusBpmnInfoService;
import cn.wolfcode.common.annotation.Log;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.core.page.TableDataInfo;
import cn.wolfcode.common.enums.BusinessType;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 流程定义明细Controller
 * 
 * @author wolfcode
 * @date 2025-08-26
 */
@RestController
@RequestMapping("/business/bpmnInfo")
public class BusBpmnInfoController extends BaseController
{
    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;

    /**
     * 查询流程定义明细列表
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusBpmnInfo busBpmnInfo)
    {
        startPage();
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 获取流程定义明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(busBpmnInfoService.selectBusBpmnInfoById(id));
    }

    /**
     * 新增流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:add')")
    @Log(title = "流程定义明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(MultipartFile file,Integer bpmnType,String info) throws IOException {
       /* System.out.println("文件原始名字" + file.getOriginalFilename());
        System.out.println("文件控件名字" + file.getName());
        System.out.println("文件类型" + file.getContentType());
        System.out.println("文件大小" + file.getSize());
        System.out.println("文件输入流" + file.getInputStream());
        System.out.println("文件类型" + bpmnType);
        System.out.println("文件信息" + info);*/
        return toAjax(busBpmnInfoService.deploy(file,bpmnType,info));
    }

    /**
     * 修改流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:edit')")
    @Log(title = "流程定义明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusBpmnInfo busBpmnInfo)
    {
        return toAjax(busBpmnInfoService.updateBusBpmnInfo(busBpmnInfo));
    }

    /**
     * 撤销流程定义明细
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:remove')")
    @Log(title = "流程定义明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busBpmnInfoService.deleteBusBpmnInfoByIds(ids));
    }

    /**
     * 查看流程资源（xml || png）
     */
    @GetMapping("/{id}/resources")
    public void readResources(@PathVariable Long id,String type, HttpServletResponse response) throws Exception {
        InputStream inputStream = busBpmnInfoService.readResources(id,type);
        // 调用工具类的方法将我们的数据流通过 response 对象响应到前端浏览器。
        IOUtils.copy(inputStream,response.getOutputStream());
    }
}
